package pl.edu.mimuw.irs.rcp.persistence;

import pl.edu.mimuw.irs.core.model.design.net.IRSWorkflow;
import pl.edu.mimuw.irs.core.model.design.resource.Resources;
import pl.edu.mimuw.irs.core.model.design.resource.Roles;
import pl.edu.mimuw.irs.rcp.persistence.resource.ResourcesPersistence;
import pl.edu.mimuw.irs.rcp.persistence.role.RolesPersistence;

/**
 * @author Jakub Rauch
 * Created on: 2009-06-29
 */
public class PersistenceAdapter {
	
	// *** Static attributes
	
	@SuppressWarnings("unchecked")
	private static IPersistence irsWorkflowPersistence;
	@SuppressWarnings("unchecked")
	private static IPersistence resourcesPersistence;
	@SuppressWarnings("unchecked")
	private static IPersistence rolesPersistence;
	@SuppressWarnings("unchecked")
	private static IPersistence getIrsWorkflowPersistence() {
		if (irsWorkflowPersistence == null) 
			irsWorkflowPersistence = new IRSWorkflowPersistence();
		return irsWorkflowPersistence;
	}
	@SuppressWarnings("unchecked")
	private static IPersistence getResourcesPersistence() {
		if (resourcesPersistence == null) 
			resourcesPersistence = new ResourcesPersistence();//new SerializablePersistence<Resources>();
		return resourcesPersistence;
	}
	@SuppressWarnings("unchecked")
	private static IPersistence getRolesPersistence() {
		if (rolesPersistence == null) 
			rolesPersistence = new RolesPersistence();
		return rolesPersistence;
	}

	// *** Static methods
	
	
	@SuppressWarnings("unchecked")
	public static <T extends Object> IPersistence<T> getManager(Class<T> clazz) {
		if (IRSWorkflow.class.isAssignableFrom(clazz)) {
			return getIrsWorkflowPersistence();
		} else if (Resources.class.isAssignableFrom(clazz)) {
			return getResourcesPersistence();
		} else if (Roles.class.isAssignableFrom(clazz)) {
			return getRolesPersistence();
		} else {
			return new SerializablePersistence<T>();
		}
	}
}
